package com.likan.chqjtgc.utils;

import com.alibaba.fastjson.JSONObject;
import com.likan.chqjtgc.commons.AuthLoginClientConfig;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.UnsupportedEncodingException;


/**
 * 获取当前登录用户 ,从token中获取当前登录用户信息
 */
@Slf4j
public final class SecurityUtils {

    /**
     * 获取当前用户登录名称/手机号码
     * @return
     */
    public static String getCurrentUserUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(authentication.getDetails()));
        String  token=jsonObject.getString("tokenValue");
        Claims claims = null;
        try {
            claims = Jwts.parser().setSigningKey(AuthLoginClientConfig.JWT_SECRET_KEY.getBytes("UTF-8")).parseClaimsJws(token).getBody();
            log.info("token包含的内容:"+JSONObject.toJSONString(claims));
            return String.valueOf(claims.get("user_name"));
        } catch (UnsupportedEncodingException e) {
            log.error("token在资源服务器解析失败!");
            log.error(e.getMessage());
            throw new AuthenticationServiceException("token解析失败!");
        }
    }

    /**
     * 获取当前用户ID
     * @return
     */
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(authentication.getDetails()));

       String  token=jsonObject.getString("tokenValue");
        Claims claims = null;
        try {
            claims = Jwts.parser().setSigningKey(AuthLoginClientConfig.JWT_SECRET_KEY.getBytes("UTF-8")).parseClaimsJws(token).getBody();
            log.info("token包含的内容:"+JSONObject.toJSONString(claims));
            return Long.valueOf(JSONObject.toJSONString(claims.get("id")));
        } catch (UnsupportedEncodingException e) {
            log.error("token在资源服务器解析失败!");
            log.error(e.getMessage());
            throw new AuthenticationServiceException("token解析失败!");
        }
    }

    /**
     * 获取当前用户I部门D
     * @return
     */
    public static Long getCurrentDeptId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(authentication.getDetails()));

        String  token=jsonObject.getString("tokenValue");
        Claims claims = null;
        try {
            claims = Jwts.parser().setSigningKey(AuthLoginClientConfig.JWT_SECRET_KEY.getBytes("UTF-8")).parseClaimsJws(token).getBody();
            log.info("token包含的内容:"+JSONObject.toJSONString(claims));
            return Long.valueOf(JSONObject.toJSONString(claims.get("deptId")));
        } catch (UnsupportedEncodingException e) {
            log.error("token在资源服务器解析失败!");
            log.error(e.getMessage());
            throw new AuthenticationServiceException("token解析失败!");
        }
    }

}
